package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.flight.entity.SkInfo;
import cn.buk.tms.entity.misc.FuelSurcharge;
import cn.buk.tms.entity.misc.ProductDefaultSupplier;
import cn.buk.qms.service.BasicDataService;
import cn.buk.qms.service.EnterpriseService;
import cn.buk.qms.service.FlightCharterService;
import cn.buk.qms.service.FlightService;
import cn.buk.tms.dto.AirlinesDto;
import cn.buk.tms.dto.AirportDto;
import cn.buk.tms.dto.CityDto;
import cn.buk.tms.dto.FlightCondTimeWindowDto;
import cn.buk.tms.flight.dto.LuggageInfoDto;
import cn.buk.tms.flight.dto.SubclassInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 基础数据管理
 * @author yfdai
 */
@RestController
public class BasicDataController extends BaseController {

  @Autowired
  public BasicDataController(EnterpriseService enterpriseService,
                              FlightService flightService,
                              FlightCharterService flightCharterService,
                             BasicDataService basicDataService) {
    this.enterpriseService = enterpriseService;
    this.flightService = flightService;
    this.flightCharterService = flightCharterService;
    this.basicDataService = basicDataService;
  }

  private final BasicDataService basicDataService;

  private final EnterpriseService enterpriseService;

  private final FlightService flightService;

  private final FlightCharterService flightCharterService;

  /**
   * 查找机场代码列表
   */
  @RequestMapping("/data/airports")
  public CommonDto<AirportDto> searchAirports(HttpServletRequest request,
                                              @ModelAttribute("sc") CommonSearchCriteria sc,
                                              @RequestParam(value = "threeCode", required = false) String threeCode,
                                              @RequestParam(value = "countryCode", required = false) String countryCode,
                                              @RequestParam(value = "name", required = false) String name
  ) {
    final int enterpriseId = getEnterpriseId(request);

    //暂时用这2个表示
    sc.setDport(threeCode);
    sc.setCarrier(countryCode);
    sc.setName(name);

    List<AirportDto> dtos = basicDataService.searchAirports(enterpriseId, sc);

    CommonDto<AirportDto> commonDto = new CommonDto<>();
    commonDto.setDataList(dtos);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 查找机场代码列表
   */
  @RequestMapping("/data/cn-airports")
  public JsonResult searchCnAirports(HttpServletRequest request, HttpServletResponse response) {
    response.setCharacterEncoding("UTF-8");
    String aa = "中文：" + enterpriseService.searchCnAirports();

    return JsonResult.createJsonResult(1, aa);
  }

  /**
   * 保存机场代码信息
   */
  @PostMapping(value = "/data/airport")
  public JsonResult createAirport(HttpServletRequest request,
                                  @RequestParam(value = "threeCode") String threeCode,
                                  @RequestParam(value = "countryCode") String countryCode,
                                  @RequestParam(value = "cityCode") String cityCode,
                                  @RequestParam(value = "cityName") String cityName,
                                  @RequestParam(value = "airportName") String airportName,
                                  @RequestParam(value = "airportEnglishName") String airportEnglishName
  ) {
    AirportDto dto = new AirportDto();
    dto.setThreeCode(threeCode);
    dto.setCountryCode(countryCode);
    dto.setCityCode(cityCode);
    dto.setCityName(cityName);
    dto.setAirportName(airportName);
    dto.setAirportEnglishName(airportEnglishName);

    return basicDataService.createAirport(getEnterpriseId(request), dto);
  }



  /**
   * 查找航空公司代码列表
   */
  @GetMapping("/data/airlines")
  public CommonDto<AirlinesDto> searchAirlines(HttpServletRequest request,
                                               @ModelAttribute("sc") CommonSearchCriteria sc,
                                               @RequestParam(value = "threeCode", required = false) String threeCode,
                                               @RequestParam(value = "twoCode", required = false) String twoCode
  ) {
    final int enterpriseId = getEnterpriseId(request);

    //暂时用这2个表示
    sc.setDport(threeCode);
    sc.setCarrier(twoCode);

    List<AirlinesDto> dtos = enterpriseService.searchAirlines(enterpriseId, sc);

    CommonDto<AirlinesDto> commonDto = new CommonDto<>();
    commonDto.setDataList(dtos);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 保存航空公司信息信息
   */
  @PostMapping(value = "/data/airlines")
  public JsonResult createAirline(
          HttpServletRequest request,
          @RequestParam(value = "id", defaultValue = "0") int id,
          @RequestParam(value = "threeCode") String threeCode,
          @RequestParam(value = "twoCode") String twoCode,
          @RequestParam(value = "name") String name,
          @RequestParam(value = "englishName") String englishName,
          @RequestParam(value = "nickName") String nickName,
          @RequestParam(value = "englishNickName") String englishNickName
  ) {
    AirlinesDto dto = new AirlinesDto();
    dto.setId(id);
    dto.setTwoCode(twoCode);
    dto.setThreeCode(threeCode);
    dto.setName(name);
    dto.setEnglishName(englishName);
    dto.setNickName(nickName);
    dto.setEnglishNickName(englishNickName);

    return basicDataService.createAirlines(getEnterpriseId(request), dto);
  }


  /**
   * 查找航空公司的舱位信息
   */
  @GetMapping("/data/subclasses")
  public CommonDto<SubclassInfoDto> searchSubclassInfos(HttpServletRequest request,
                                                        @ModelAttribute("sc") CommonSearchCriteria sc) {
    final int enterpriseId = getEnterpriseId(request);

    List<SubclassInfoDto> infos = flightCharterService.searchSubclassInfos(enterpriseId, sc);

    CommonDto<SubclassInfoDto> dto = new CommonDto<>();
    dto.setDataList(infos);
    dto.setPage(sc.getPage());

    return dto;
  }

  /**
   * 保存航空公司的舱位信息
   * @param request
   * @param subclassInfo
   * @return
   */
  @PostMapping(value = "/data/subclass")
  public JsonResult saveSubclassInfo(HttpServletRequest request,
                                     @ModelAttribute SubclassInfoDto subclassInfo) {
    return flightCharterService.saveSubclassInfo(getEnterpriseId(request), subclassInfo);
  }

  /**
   * 获取航空公司的舱位信息
   * @param request
   * @param id
   * @return
   */
  @GetMapping(value = "/data/subclass/{id}")
  public SubclassInfoDto searchSubclassInfo(HttpServletRequest request,
                                     @PathVariable("id") int id) {
    return flightCharterService.searchSubclassInfoById(getEnterpriseId(request), id);
  }


  /**
   * 查找航空公司的退改时间窗口信息
   */
  @GetMapping("/data/flightConditionTimeWindow")
  public List<FlightCondTimeWindowDto> searchFlightConditionTimeWindows(HttpServletRequest request,
                                                                        @RequestParam("carrier") String carrier) {
    return basicDataService.searchFlightConditionTimeWindows(carrier.trim().toUpperCase());
  }

  /**
   * 保存/修改 航空公司用于自愿退改的时间窗口信息
   */
  @PostMapping(value = "/data/flightConditionTimeWindow", consumes = "application/json")
  public JsonResult saveFlightConditionTimeWindows(HttpServletRequest request,
                                                   @RequestBody List<FlightCondTimeWindowDto> list) {
    return basicDataService.saveFlightConditionTimeWindows(getUserId(request), list);
  }


  /**
   * 查找航班时刻表
   */
  @RequestMapping("/data/airline/schedules")
  public CommonDto<SkInfo> searchAirlineSchedules(HttpServletRequest request,
                                                  @ModelAttribute("sc") CommonSearchCriteria sc,
                                                  @RequestParam(value = "dcity", required = false) String dcity,
                                                  @RequestParam(value = "acity", required = false) String acity,
                                                  @RequestParam(value = "flightNo", required = false) String flightNo
  ) {

    //暂时用这2个表示
    sc.setDport(dcity);
    sc.setAport(acity);
    sc.setCarrier(flightNo);

    List<SkInfo> dtos = flightService.searchAirlineSchedules(sc);

    CommonDto<SkInfo> commonDto = new CommonDto<>();
    commonDto.setDataList(dtos);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 查找燃油附加费设置
   */
  @RequestMapping("/data/fuelSurcharges")
  public CommonDto<FuelSurcharge> searchFuelSurcharge(HttpServletRequest request,
                                                      @ModelAttribute("sc") CommonSearchCriteria sc) {
    final int enterpriseId = getEnterpriseId(request);

    List<FuelSurcharge> dtos = enterpriseService.searchFuelSurcharges(enterpriseId, sc);

    CommonDto<FuelSurcharge> commonDto = new CommonDto<>();
    commonDto.setDataList(dtos);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 创建燃油附加费
   */
  @PostMapping(value = "/data/fuelSurcharge")
  public JsonResult createFuelSurcharge(HttpServletRequest request,
                                        @RequestParam("mileageType") int mileageType,
                                        @RequestParam("effectDate") Date effectDate,
                                        @RequestParam("surcharge") int surcharge) {
    final int enterpriseId = getEnterpriseId(request);

    return enterpriseService.createFuelSurcharge(enterpriseId, mileageType, effectDate, surcharge);
  }


  /**
   * 删除燃油附加费
   */
  @DeleteMapping(value = "/data/fuelSurcharge/{id}")
  public JsonResult deleteFuelSurcharge(HttpServletRequest request,
                                        @PathVariable("id") int id) {
    final int enterpriseId = getEnterpriseId(request);

    return enterpriseService.deleteFuelSurcharge(enterpriseId, id);
  }

  /**
   * 查找城市信息列表
   */
  @RequestMapping("/data/cities")
  public CommonDto<CityDto> searchCities(HttpServletRequest request,
                                         @ModelAttribute("sc") CommonSearchCriteria sc,
                                         @RequestParam(value = "name", required = false) String name
  ) {
    final int enterpriseId = getEnterpriseId(request);

    sc.setName(name);

    List<CityDto> dtos = basicDataService.searchCities(enterpriseId, sc);

    CommonDto<CityDto> commonDto = new CommonDto<>();
    commonDto.setDataList(dtos);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 保存城市信息
   */
  @PostMapping(value = "/data/city")
  public JsonResult createCity(HttpServletRequest request,
                                  @RequestParam(value = "id") int id,
                                  @RequestParam(value = "name") String name,
                                  @RequestParam(value = "englishName") String englishName,
                                  @RequestParam(value = "cityCode") String cityCode,
                                  @RequestParam(value = "countryCode") String countryCode,
                                  @RequestParam(value = "countryName") String countryName,
                                  @RequestParam(value = "provinceName") String provinceName
                               ) {
    CityDto dto = new CityDto();
    dto.setId(id);
    dto.setCityName(name);
    dto.setCityEnglishName(englishName);
    dto.setCityCode(cityCode);
    dto.setProvinceName(provinceName);
    dto.setCountryCode(countryCode);
    dto.setCountryName(countryName);

    return basicDataService.createCity(getEnterpriseId(request), getUsername(request), dto);
  }

  @GetMapping(value = "/api-product-suppliers")
  public List<ProductDefaultSupplier> searchApiProductSupplier(HttpServletRequest request) {
    return enterpriseService.searchApiProductSupplier(getEnterpriseId(request));
  }

  /**
   * 保存航空公司的行李信息
   * @param request
   * @param luggageInfo
   * @return
   */
  @PostMapping(value = "/data/luggage")
  public JsonResult saveLuggageInfo(HttpServletRequest request,
                                     @ModelAttribute LuggageInfoDto luggageInfo) {
    return basicDataService.saveLuggageInfo(getEnterpriseId(request), luggageInfo);
  }

  /**
   * 查找航空公司的行李信息
   */
  @GetMapping("/data/luggages")
  public CommonDto<LuggageInfoDto> searchLuggageInfos(HttpServletRequest request,
                                                        @ModelAttribute("sc") CommonSearchCriteria sc) {
    final int enterpriseId = getEnterpriseId(request);

    List<LuggageInfoDto> infos = basicDataService.searchLuggageInfos(enterpriseId, sc);

    CommonDto<LuggageInfoDto> dto = new CommonDto<>();
    dto.setDataList(infos);
    dto.setPage(sc.getPage());

    return dto;
  }

  /**
   * 查找航空公司的行李信息
   */
  @GetMapping("/data/luggage/{id}")
  public LuggageInfoDto searchLuggageInfo(HttpServletRequest request,
                                                      @PathVariable("id") int id) {
    final int enterpriseId = getEnterpriseId(request);

    return basicDataService.searchLuggageInfo(enterpriseId, id);

  }

}
